package com.heliang.dataaccess.db;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalDb;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.heliang.common.FileLog;
import com.heliang.dataaccess.bean.VersionBean;
import com.heliang.dataaccess.dao.VersionDao;
import com.heliang.dataaccess.dao.impl.VersionBeanDaoImpl;
import com.heliang.util.Constants;
import com.hl.component.exception.DaoException;

/**
 * @ClassName: DataBaseHelper.java
 * @Description: 管理和维护数据库
 * @author heliang
 * @version V1.0
 * @Date 2013-9-4 上午7:40:11
 */
public class DataBaseHelper extends SQLiteOpenHelper {
    // 用于打印log
    private static final String TAG = "DataBaseHelper";

    // 数据库名称
    public static final String DB_NAME = "APP_XIEHUI2";

    // 数据库版本
    private static final int DB_VERSION = 1;

    /********************************************* 表名称 ************************************************/
    /**
     * 版本表名称
     */
    public static String T_VERSION = "t_version";

    /**
     * 资讯分类表名称
     */
    public static String T_INFO_CAT = "t_info_cat";

    /**
     * 资讯版本表
     */
    public static String T_INFO_VIERSION = "t_info_version";

    /**
     * 资讯表
     */
    public static String T_INFO = "t_info";

    /**
     * 热点顶部广告
     */
    public static String T_ADV_TOP = "t_adv_top";

    /**
     * 热点底部广告
     */
    public static String T_ADV_FOOT = "t_adv_foot";

    /**
     * 会员表
     */
    public static String T_MEMBER = "t_member";

    /**
     * 通讯录的会员表
     */
    public static String T_MEMBER_BOOK = "t_member_book";

    /********************************************* 建表语句 ************************************************/
    /**
     * 创建版本表sql
     */
    private String cTVersion;

    /**
     * 创建资讯分类表sql
     */
    private String cTInfoCat;

    /**
     * 创建资讯表sql
     */
    private String cTInfoSql;

    /**
     * 初始化版本信息sql
     */
    private String initVersion;

    /**
     * 创建资讯版本
     */
    private String cTInfoVersionSql;

    /**
     * 创建顶部热点广告sql
     */
    private String cTAdvSql;

    /**
     * 创建底部热点广告sql
     */
    private String cTAdvFooterSql;

    /**
     * 创建会员表sql
     */
    private String cTMemberSql;

    /**
     * 会员通讯录表sql
     */
    private String cTMemberBookSql;

    private Context context;

    private VersionBeanDaoImpl versionDao;

    public DataBaseHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.context = context;
        versionDao = new VersionBeanDaoImpl(context);
    }

    /**
     * 资讯分类版本ID
     */
    public static final int VER_INFO_CAT = 1;

    /**
     * 资讯版本ID
     */
    public static final int VER_INFO = 2;

    /**
     * 首页推荐资讯版本ID
     */
    public static final int VER_RECOMMEND_INFO = 3;

    /**
     * 顶部广告版本ID
     */
    public static final int VER_ADVERTISEMENT = 4;

    /**
     * 会员版本ID
     */
    public static final int VER_MEMBER = 5;

    /**
     * 产品分类版本ID
     */
    public static final int VER_PRODUCT_CAT = 6;

    @Override
    public void onCreate(SQLiteDatabase db) {
        VersionBean verInfoCat = new VersionBean(Constants.VER_INFO_CAT, 0, "t_table_infoCat", "");
        VersionBean verInfo = new VersionBean(Constants.VER_INFO, 0, "t_table_info", "");
        VersionBean verRecommendInfo = new VersionBean(Constants.VER_RECOMMEND_INFO, 0, "t_table_info", "");
        VersionBean verAdvertisement = new VersionBean(Constants.VER_ADVERTISEMENT, 0, "t_table_adv", "");
        VersionBean verMember = new VersionBean(Constants.VER_MEMBER, 0, "t_table_member", "");
        VersionBean verProductCat = new VersionBean(Constants.VER_PRODUCT_CAT, 0, "t_table_productCat", "");

        try {
            versionDao.saveVersionBeans(verInfoCat);
            versionDao.saveVersionBeans(verInfo);
            versionDao.saveVersionBeans(verRecommendInfo);
            versionDao.saveVersionBeans(verAdvertisement);
            versionDao.saveVersionBeans(verMember);
            versionDao.saveVersionBeans(verProductCat);
        } catch (DaoException e) {
            String errorMsg = "插入数据库版本信息失败";
            if (e.getCause() != null) {
                Log.e(Constants.TAG, e.getCause().getMessage()+errorMsg);
                FileLog.log(" DataBaseHelper.onCreate " + e.getCause().getMessage()+errorMsg);
            } else if (e.getMessage() != null) {
                Log.e(Constants.TAG, e.getMessage()+errorMsg);
                FileLog.log(" DataBaseHelper.onCreate " + e.getMessage()+errorMsg);
            }
        }

        // try {
        // // versionDao.saveVersions(versions);
        // } catch (DaoException e) {
        //
        // if (e.getCause() != null) {
        // Log.e(Constants.TAG, e.getCause().getMessage());
        // FileLog.log(" DataBaseHelper.onCreate " + e.getCause().getMessage());
        // } else if (e.getMessage() != null) {
        // Log.e(Constants.TAG, e.getMessage());
        // FileLog.log(" DataBaseHelper.onCreate " + e.getMessage());
        // }
        // }
    }

    /**
     * 升级数据库
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}
